package com.gitee.wsl.struct.tree.sample.binary.ext

import com.gitee.wsl.struct.tree.sample.binary.MutableBinaryTreeNode


/**
 * Performs the following transformation on the given MutableBinaryTreeNode:
 * <pre>
 * o1                    o2
 * / \                   / \
 * A   o2     ====>     o1   C
 * / \              / \
 * B   C            A   B
</pre> *
 *
 * @param node the node to transform
 * @return the new root after the transformation, which is either the right sub node of the original root
 * or the original root, if the right sub node is null
 */
fun <T,N : MutableBinaryTreeNode<T,N>> N.toLeftAssociativity(): N {
    val right = rightNode ?: return this

    rightNode = right.leftNode
    right.leftNode = this
//    node.setRight(right.left())
//    right.setLeft(node)
    return right
}

fun <T,N : MutableBinaryTreeNode<T,N>> N.toRightAssociativity(): N {
    val left = leftNode ?: return this

    leftNode = left.rightNode
    left.rightNode = this
//    node.setRight(right.left())
//    right.setLeft(node)
    return left
}